package edu.hdu.android.sudoku;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.Arrays;

@SuppressLint("Registered")
public class GameActivity extends AppCompatActivity {

    private static final String TAG = "GameActivity";
    public static String str;
    public static String loadStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        //去掉标题栏
//        ActionBar actionBar = getSupportActionBar();
//        actionBar.hide();
//        //去掉状态栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        str = getResources().getStringArray(R.array.str)[MyApplication.difficulty];
        if (MyApplication.flag != 0) {
            Log.d(TAG, "onCreate: load");
            loadStr = load();
        }
        setContentView(R.layout.activity_game);
    }

    @Override
    public void onBackPressed() {
        @SuppressLint("CommitPrefEdits") SharedPreferences.Editor editor =
                this.getSharedPreferences("GameLoad", MODE_PRIVATE).edit();
            String str = getStr(Arrays.toString(MyApplication.sudoku));
        switch (MyApplication.difficulty) {
            case 0:
                editor.putString("d1", str);
                break;
            case 1:
                editor.putString("d2", str);
                break;
            case 2:
                editor.putString("d3", str);
                break;
            default:
        }
        editor.apply();
        Toast.makeText(this, "游戏已保存", Toast.LENGTH_SHORT).show();
        finish();
        super.onBackPressed();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.music:
                if(MyApplication.isRelease){
                    Toast.makeText(this, "播放音乐", Toast.LENGTH_SHORT)
                            .show();
                    MyApplication.mPlayer = MediaPlayer.create(this, R.raw.music);
                    MyApplication.mPlayer.start();   //开始播放
                    MyApplication.isRelease = false;
                } else {
                    Toast.makeText(this, "关闭音乐", Toast.LENGTH_SHORT)
                            .show();
                    MyApplication.mPlayer.reset();     //重置MediaPlayer
                    MyApplication.mPlayer.release();   //释放MediaPlayer
                    MyApplication.isRelease = true;
                }
                return true;
            case R.id.hints:
                Toast.makeText(this, R.string.point, Toast.LENGTH_LONG).show();
                return true;
            default: break;
        }
        return false;
    }

    //加载数据
    public String load() {
        String difficulty = "";
        switch (MyApplication.difficulty) {
            case 0:
                difficulty = "d1";
                break;
            case 1:
                difficulty = "d2";
                break;
            case 2:
                difficulty = "d3";
                break;
            default:
        }
        SharedPreferences sharedPreferences = this.getSharedPreferences("GameLoad",
                MODE_PRIVATE);
        return sharedPreferences.getString(difficulty, "");
    }

    //字符串处理
    private String getStr(String string) {
        String[] array = string.split(", ");
        StringBuilder str = new StringBuilder();
        for (String s : array) {
            str.append(s);
        }
        str = new StringBuilder(str.substring(1, str.length() - 1));
        return str.toString();
    }
}
